<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消">
    
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="任务信息">
              <a-descriptions :column="2" bordered size="small">
                <a-descriptions-item label="工种名称">{{ model.workTypeName }}</a-descriptions-item>
                <a-descriptions-item label="任务数量">{{ model.taskQuantity }}</a-descriptions-item>
                <a-descriptions-item label="预计工时">{{ model.estimatedHours }}小时</a-descriptions-item>
                <a-descriptions-item label="工序顺序">第{{ model.sequence }}道工序</a-descriptions-item>
                <a-descriptions-item label="计划开始">{{ model.planStartDate }}</a-descriptions-item>
                <a-descriptions-item label="计划结束">{{ model.planEndDate }}</a-descriptions-item>
              </a-descriptions>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="领取备注" prop="remark">
              <a-textarea v-model="model.remark" rows="3" placeholder="请输入领取备注（可选）"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { postAction } from '@/api/manage'
  import { mapGetters } from 'vuex'

  export default {
    name: 'TaskClaimModal',
    data () {
      return {
        title:"领取任务",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          claim: "/mes/task/claim"
        }
      }
    },
    computed: {
      ...mapGetters(['userInfo']),
      currentUser() {
        return this.userInfo || {}
      }
    },
    methods: {
      claim (record) {
        this.model = Object.assign({}, record);
        this.model.remark = '';
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = that.url.claim;
            let method = 'post';
            let params = {
              taskId: that.model.id,
              employeeId: that.currentUser.id,
              remark: that.model.remark
            };
            
            postAction(httpurl, params).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      handleCancel () {
        this.close();
      }
    }
  }
</script>
